//package com.pand.thread;
//
//import com.pand.mysql.model.Track;
//import com.pand.mysql.service.OrderService;
//import com.pand.mysql.service.TrackService;
//import com.pand.trajectory.service.TrajectoryService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Component;
//
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * Created by lh on 2017/11/19.
// */
//public class TrackThread extends Thread{
//    TrajectoryService trajectoryService;
//
//    TrackService trackService;
//
//    OrderService orderService;
//
//    String date;
//    int hour;
//
//    public TrackThread(String date, int hour,TrajectoryService trajectoryService,TrackService trackService,OrderService orderService) {
//        this.date = date;
//        this.hour = hour;
//        this.trajectoryService = trajectoryService;
//        this.trackService = trackService;
//        this.orderService = orderService;
//    }
//
//
//    @Override
//    public void run() {
//        String dbDate = this.date.replaceAll("-", "");
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date startTime;
//        Date endTime;
//        try {
//            startTime = dateFormat.parse(this.date + " " + String.format("%02d", hour) + ":00:00");
//            for (int i = 0; i <= 60; i++) {
//                if (i == 60) {
//                    endTime = dateFormat.parse(this.date + " " + String.format("%02d", hour) + ":59:59");
//                    List<Track> trackList = trajectoryService.findTrajectorys(new String[]{dbDate}, startTime, endTime).parallelStream()
//                            .map(object -> {
//                                Track track = new Track();
//                                track.setVin(object[0].toString());
//                                if (object[1] != null) {
//                                    track.setLongitude(Double.valueOf(object[1].toString()));
//                                }
//                                if (object[2] != null) {
//                                    track.setLatitude(Double.valueOf(object[2].toString()));
//                                }
//                                try {
//                                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                                    track.setGpsTime(format.parse(object[3].toString()));
//                                } catch (ParseException e) {
//                                    e.printStackTrace();
//                                }
//                                track.setId(object[4].toString());
//                                orderService.findFirstByVin(track.getVin())
//                                        .ifPresent(order -> track.setLicense(order.getLicense()));
//                                return track;
//                            }).collect(Collectors.toList());
//                    trackService.save(trackList);
//                    System.out.println(dateFormat.format(startTime)+"---"+dateFormat.format(endTime));
//                    startTime = endTime;
//                } else {
//                    for (int j = 2; j <= 60; j += 2) {
//                        if (j == 60) {
//                            endTime = dateFormat.parse(this.date + " " + String.format("%02d", hour) + ":" + String.format("%02d", i) + ":59");
//                        }else {
//                            endTime = dateFormat.parse(this.date + " " + String.format("%02d", hour) + ":" + String.format("%02d", i) + ":" + String.format("%02d", j));
//                        }
//
//                        List<Track> trackList = trajectoryService.findTrajectorys(new String[]{dbDate}, startTime, endTime).parallelStream()
//                                .map(object -> {
//                                    Track track = new Track();
//                                    track.setVin(object[0].toString());
//                                    if (object[1] != null) {
//                                        track.setLongitude(Double.valueOf(object[1].toString()));
//                                    }
//                                    if (object[2] != null) {
//                                        track.setLatitude(Double.valueOf(object[2].toString()));
//                                    }
//                                    try {
//                                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                                        track.setGpsTime(format.parse(object[3].toString()));
//                                    } catch (ParseException e) {
//                                        e.printStackTrace();
//                                    }
//                                    track.setId(object[4].toString());
//                                    orderService.findFirstByVin(track.getVin())
//                                            .ifPresent(order -> track.setLicense(order.getLicense()));
//                                    return track;
//                                }).collect(Collectors.toList());
//                        trackService.save(trackList);
//                        System.out.println(dateFormat.format(startTime)+"---"+dateFormat.format(endTime));
//                        startTime = endTime;
//                    }
//                }
//            }
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//    }
//}
